package com.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by kenfo on 2017/10/16.
 */
public class HelloScheduler {

    public static void main(String[] args) throws SchedulerException {
        //创建一个JobDetail实例， 将该实例与hellojob class 绑定
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob","group1").build();
        //创建一个Trigger实例，定义该job立即执行，并且每隔两秒钟重复执行一次
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger","group1")
                .startNow().withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(2).repeatForever()
                ).build();
        //创建Schedule实例
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.start();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Time is:" + simpleDateFormat.format(date));
        scheduler.scheduleJob(jobDetail,trigger);
    }
}
